package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        //1.查询当前是否存在菜品
        ShoppingCart shoppingCartCondition = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCartCondition);
        shoppingCartCondition.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.select(shoppingCartCondition);

        // 2.查询购物车里是否当前商品，更新数量+1
        if (shoppingCarts != null && shoppingCarts.size() > 0) {
            // 更新商品数量
            ShoppingCart shoppingCart = shoppingCarts.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumber(shoppingCart);
        } else {
            //3.没有商品 ，则添加商品
            // 判断是否为套餐
            if (shoppingCartDTO.getDishId() != null) {
                // 菜品
                Dish dish = dishMapper.getDish(String.valueOf(shoppingCartDTO.getDishId()));

                // 添加菜品
                shoppingCartCondition.setImage(dish.getImage());
                shoppingCartCondition.setAmount(dish.getPrice());
                shoppingCartCondition.setName(dish.getName());
            } else {
                // 获取套餐
                Setmeal setmealById = setmealMapper.getSetmealById(String.valueOf(shoppingCartCondition.getSetmealId()));
                shoppingCartCondition.setImage(setmealById.getImage());
                shoppingCartCondition.setAmount(setmealById.getPrice());
                shoppingCartCondition.setName(setmealById.getName());

            }

            shoppingCartCondition.setNumber(1);
            shoppingCartCondition.setCreateTime(LocalDateTime.now());

            shoppingCartMapper.insertThings(shoppingCartCondition);
        }

    }

    /**
     * 查看购物车
     * @param userId
     * @return
     */
    @Override
    public List<ShoppingCart> listCart(Long userId) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        return shoppingCartMapper.select(shoppingCart);

    }

    /**
     * 情况购物车
     * @param userId
     */
    @Override
    public void deleteCart(Long userId) {
        shoppingCartMapper.deleteCart(userId);
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void subCart(ShoppingCartDTO shoppingCartDTO) {
        //1.查询当前是否存在菜品
        ShoppingCart shoppingCartCondition = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCartCondition);
        shoppingCartCondition.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.select(shoppingCartCondition);
        // 2.如果存在 判断是否为1
        for (ShoppingCart shoppingCart : shoppingCarts) {
            if(shoppingCart.getNumber().equals(1)){
                shoppingCartMapper.deleteCartById(shoppingCart.getId());
            }else {
                Integer number = shoppingCart.getNumber();
                shoppingCart.setNumber(number-1);
                shoppingCartMapper.updateNumber(shoppingCart);
            }
        }
    }
}
